#   xpath解析实例1.爬取全国城市名
#   学者 小韦
#   2022年7月3日 阵雨 湖南娄底娄星区
import os.path

import requests
from lxml import etree
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/101.0.0.0 Safari/537.36'}
# url = 'https://www.aqistudy.cn/historydata/'
# page_text = requests.get(url=url,headers=header).text
#
# tree = etree.HTML(page_text)
# all_city_list = []

# li_list = tree.xpath('//div[@class="bottom"]/ul/li')
# for li in li_list:
#     hot_city_name = li.xpath('./a/text()')[0]
#     # all_city_list.append(hot_city_name)
#
#
# ul_list = tree.xpath('//div[@class="bottom"]/ul')
# for ul in ul_list:
#     total_city_li = ul.xpath('./div[2]/li')
#     for li in total_city_li:
#         total_city_name = li.xpath('./a/text()')[0]
#         all_city_list.append(total_city_name)
# print(all_city_list,len(all_city_list))

#两种不同层级都能定位到
# a_list = tree.xpath('//div[@class="bottom"]/ul/li/a | //div[@class="bottom"]/ul/div[2]/li/a')
# for a in a_list:
#     all_city_name = a.xpath('./text()')[0]
#     all_city_list.append(all_city_name)
# print(all_city_list,len(all_city_list))


#   xpath解析实例2.作业爬取站长资源免费ppt模板
if not os.path.exists('./ppt模板'):
    os.mkdir('./ppt模板')
#记一下下载个数
total=0

url_ppt = 'https://sc.chinaz.com/jianli/free.html'
page_ppt_text = requests.get(url=url_ppt,headers=header).content
tree_ppt = etree.HTML(page_ppt_text)
#先下载第一页
div_list = tree_ppt.xpath('//div[@id="container"]/div')
for div in div_list:
    ppt_href = 'https:'+div.xpath('./a/@href')[0]
    ppt_name = div.xpath('./a/img/@alt')[0]+'.rar'

    ppt_href_text = requests.get(url=ppt_href,headers=header).text
    tree_ppt_down = etree.HTML(ppt_href_text)
    ppt_down_href = tree_ppt_down.xpath('//div[@id="down"]/div[2]/ul/li[3]/a/@href')[0]
    #下载压缩文件
    ppt_data = requests.get(url=ppt_down_href,headers=header).content
    ppt_path = 'ppt模板/'+ppt_name
    with open(ppt_path,'wb') as fp:
        fp.write(ppt_data)
        total = total+1
        print(ppt_name,'下载成功'+str(total)+'个文件')
#从第二页开始循环
count = 0
while count < 2:
    count = count+1
    Page_number_url = 'https://sc.chinaz.com/jianli/'+tree_ppt.xpath('//a[@class="nextpage"]/@href')[0]
    page_ppt_text = requests.get(url=Page_number_url, headers=header).content
    tree_ppt = etree.HTML(page_ppt_text)
    div_list = tree_ppt.xpath('//div[@id="container"]/div')

    for div in div_list:
        ppt_href = 'https:'+div.xpath('./a/@href')[0]
        ppt_name = div.xpath('./a/img/@alt')[0]+'.rar'

        ppt_href_text = requests.get(url=ppt_href,headers=header).text
        tree_ppt_down = etree.HTML(ppt_href_text)
        ppt_down_href = tree_ppt_down.xpath('//div[@id="down"]/div[2]/ul/li[3]/a/@href')[0]
        #下载压缩文件
        ppt_data = requests.get(url=ppt_down_href,headers=header).content
        ppt_path = 'ppt模板/'+ppt_name
        with open(ppt_path,'wb') as fp:
            fp.write(ppt_data)
            total = total + 1
            print(ppt_name, '下载成功' + str(total) + '个文件')


#//*[@id="container"]/div[1]

